﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using QualityEvaluatingTools.model;
using Aspose.Cells;
using System.IO;
using QualityEvaluatingTools.helper;

namespace QualityEvaluatingTools
{
    public partial class MainForm : Form
    {
        #region 自定义变量

        public static List<DataModel> dataModelList;

        public static List<Evaluation> evaluationList;

        #endregion

        #region load事件以及构造函数

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// load事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        #endregion

        #region 控件事件

        /// <summary>
        /// 导入数据单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbImportData_Click(object sender, EventArgs e)
        {
            if (this.openFileDialogOfExcelData.ShowDialog(this) == DialogResult.OK)
            {
                string excelPath = this.openFileDialogOfExcelData.FileName;
                try
                {
                    this.Calculate(excelPath);
                    this.tsbOutTotal.Enabled = true;
                    this.tsbStuMsg.Enabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("统计失败：" + ex.Message);
                }

            }
        }

        /// <summary>
        /// 导出综合评测分析表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbOutTotal_Click(object sender, EventArgs e)
        {
            this.saveFileDialogData.FileName = "综合素养与职业性格分析总体评估结果统计分析结果.xls";
            if (this.saveFileDialogData.ShowDialog(this) == DialogResult.OK)
            {
                try
                {
                    string excelPath = this.saveFileDialogData.FileName;
                    ExcelHelper.ExportDataGridview(this.dgvReport, excelPath);
                    MessageBox.Show("导出成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("导出失败！：" + ex.Message);
                }
            }
        }

        /// <summary>
        /// 导出学生详细测评分析
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbStuMsg_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialogStuMsg.ShowDialog(this) == DialogResult.OK)
            {
                string saveFilePath = folderBrowserDialogStuMsg.SelectedPath;
                this.SaveStuExcel(saveFilePath);
                MessageBox.Show("导出成功！");
            }
        }

        #endregion

        #region 自定义方法

        /// <summary>
        /// 系统计算测评分析
        /// </summary>
        private void Calculate(string excelPath)
        {
            FormReadFrom frm = new FormReadFrom(excelPath);
            frm.ShowDialog();
            this.dgvReport.DataSource = dataModelList;
        }

        /// <summary>
        /// 保存学员详细测评结果
        /// </summary>
        /// <param name="savePath"></param>
        private void SaveStuExcel(string savePath)
        {
            if (evaluationList == null)
                return;
            foreach (var item in evaluationList)
            {
                try
                {
                    ExcelHelper.SaveStuEvaluation(item, savePath);
                }
                catch (Exception)
                {
                }
            }
        }

        #endregion

    }
}
